﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using GBL.Repository.CoatOfArms;
using GBL.Repository.Resources;
using GBlason.ViewModel;
using GBlason.ViewModel.CoatOfArm;

namespace GBlason.Common.Converter
{
    public static class CoatOfArmConverter
    {
        #region CoatOfArms
        /// <summary>
        /// Create a new instance of view model from the repository data
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static CoatOfArmViewModel ConvertToViewModel(this CoatOfArms source)
        {
            return new CoatOfArmViewModel { OriginObject = source };
        }

        /// <summary>
        /// Converts to coat of arms.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static CoatOfArms ConvertToCoatOfArms(this CoatOfArmViewModel source)
        {
            var coa = new CoatOfArms {Shape = source.CurrentShape.ConvertToShape(), Divisions = new List<Division>()};
            foreach(var child in source.Children.OfType<DivisionViewModel>())
                coa.Divisions.Add(child.ConvertToDivision());
            return coa;
        }
        #endregion

        #region Shape
        /// <summary>
        /// Converts to view model.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <returns></returns>
        public static ShapeViewModel ConvertToViewModel(this Shape source)
        {
            return new ShapeViewModel
                            {
                                Name = source.Name,
                                Description = source.Description,
                                Geometry = source.Geometry,
                                Identifier = source.Identifier,
                                PreferedHeight = source.PathHeight,
                                PreferedWidth = source.PathWidth,
                                TypeOfResource = source.TypeOfResource
                            };
        }

        public static Shape ConvertToShape(this ShapeViewModel source)
        {
            return new Shape
                       {
                           Name = source.Name,
                           Description = source.Description,
                           Geometry = source.Geometry,
                           Identifier = source.Identifier,
                           PathHeight = source.PreferedHeight,
                           PathWidth = source.PreferedWidth,
                           TypeOfResource = source.TypeOfResource
                       };
        }
        #endregion

        #region Division

        public static DivisionViewModel ConvertToViewModel(this Division source)
        {
            return new DivisionViewModel
            {
                Geometry = source.Geometry,
                DivisionType = source.Type
            };
        }

        public static Division ConvertToDivision(this DivisionViewModel source)
        {
            return new Division
            {
                Geometry = source.Geometry,
                Type = source.DivisionType
            };
        }

        #endregion

        #region Tinctures

        public static TinctureViewModel ConvertToViewModel(this Tincture source)
        {
            var color = source as TinctureColor;
            var fur = source as TinctureFur;
            var retour = new TinctureViewModel
            {
                Identifier = source.Identifier,
                Type = source.Type,
                Name = source.Name
            };

            if (source.Type == TinctureType.Furs)
            {
                //we add a fur
                if(fur==null)
                    throw new NullReferenceException("fur");
                retour.FurColor = fur.ConvertToViewModel();
            }
            else
            {
                var bc = new BrushConverter();
                if (color == null)
                    throw new NullReferenceException("color");
                var temp = bc.ConvertFromString(color.RgbColor) as SolidColorBrush;
                if (temp == null)
                    throw new InvalidCastException("color");
                retour.TinctureColor = temp.Color;
            }

            return retour;
        }

        public static FurViewModel ConvertToViewModel(this TinctureFur source)
        {
            var bc = new BrushConverter();
            var temp1 = bc.ConvertFromString(source.BackgroundColor) as SolidColorBrush;
            var temp2 = bc.ConvertFromString(source.MainShapeColor) as SolidColorBrush;
            if (temp1 == null || temp2 == null)
                throw new InvalidCastException("fur");
            return new FurViewModel
                       {
                           BackgroundColor = temp1.Color,
                           MainShapeColor = temp2.Color,
                           MainShapeGeometry = source.MainShapeGeometry,
                           TypeFur = source.TypeFur
                       };
        }

        //public static Tincture ConvertToTincture(this TinctureViewModel source)
        //{
        //    var retour = new Tincture
        //                     {
        //                         Identifier = source.Identifier,
        //                         Name = source.Name,
        //                         Type = source.Type
        //                     };
        //}

        #endregion
    }
}
